class StartGame extends eui.Component implements eui.UIComponent {
	public constructor() {
		super();
		this.skinName = "StartGameSkin";
	}

	protected partAdded(partName: string, instance: any): void {
		super.partAdded(partName, instance);
	}

	private qiua;
	private qiub;
	private qiuc;
	private shou;
	private shuai;
	private jindu;	//进度条
	private time;
	private qa;
	static initKm = 0; 	//发球距离
	private sound:egret.Sound;
	private soundChannel:egret.SoundChannel;

	protected childrenCreated(): void {
		super.childrenCreated();
		egret.Tween.get(this.qiua, { loop: true }).to({ alpha: 0 }, 500).to({ alpha: 0.6 }, 1200);
		egret.Tween.get(this.qiub, { loop: true }).to({ alpha: 0 }, 500).to({ alpha: 0.8 }, 900);
		this.playAnimation(this.shuai, true);

		if (window && window["DeviceMotionEvent"]) {
			var second = 5000;//秒
			var starttime = egret.getTimer();//开始时间
			var startX, startY, startZ, endX, endY, endZ;//开始坐标和结束坐标
			startX = startY = startZ = endX = endY = endZ = 0;
			window.addEventListener('devicemotion', (e) => {
				var acceleration = e.accelerationIncludingGravity;//
				var endtime = egret.getTimer();//结束时间
				if ((endtime - starttime) > 100) {
					var diffTime = endtime - starttime;//时间差
					startX = acceleration.x;
					startY = acceleration.y;
					startZ = acceleration.z;
					var speed = Math.abs(startX + startY + startZ - endX - endY - endZ) / diffTime * 10000;
					if (speed > second) {
						this.jindu.width += 0.02 * 234;
						StartGame.initKm += 0.02* 1000;
						if (this.jindu.width >= 234) {
							this.jindu.width = 234;
							StartGame.initKm = 1000;
						}
						//这里就可以写你的操作了,如摇一摇抽奖,换个背景图等,不解释了
					}
					starttime = endtime;
					endX = startX;
					endY = startY;
					endZ = startZ;
				}
			}, false);

		} else {
			alert('不支持传感器事件');
		}

		setTimeout(()=>{
			this.time.text = 5;
			//定时器
			var timer = new egret.Timer(1000,5);
			timer.addEventListener(egret.TimerEvent.TIMER,()=>{
				this.time.text--;
				if(this.time.text==0){
					this.time.text="";
					this.qiua.width = 0;
					this.qiub.width = 0;
					this.qa.play();
					Index.soundChannel.stop();
			        this.sound = RES.getRes("wangqiu_mp3"); 
                    this.soundChannel = this.sound.play(0,1);
					this.qa.addEventListener(egret.Event.COMPLETE,function(){
						SceneManager.createObj(this.stage).loadScene("play",Play);
					},this)
					
				}
			},this)
			timer.start();
		},1500)
	}

	设置循环播放
	private playAnimation(target: egret.tween.TweenGroup, isLoop: boolean): void {
		if (isLoop) {
			for (var key in target.items) {
				target.items[key].props = { loop: true};
			}
		}
		target.play();
	}
}
